package HashMapSeries;

import java.util.HashSet;
import java.util.Set;

public class LongestConsecutiveSolution_128 {
    public int longestConsecutive(int[] nums) {
        // 首先对nums去重 Set不重复
        Set<Integer> set = new HashSet<>();
        for (Integer num : nums) {
            set.add(num);
        }

        // curRes -- 当前长度
        // longRes -- 最大长度
        // 遍历数组，判断nums[i]-1在不在数组中。如果不在 长度置1，如果在 curRes+1

        int longRes = 0;
        for (int num : nums) {
            if (!set.contains(num - 1)) {
                int curNum = num;
                int curRes = 1;

                while (set.contains(curNum + 1)) {
                    curNum += 1;
                    curRes += 1;
                }

                longRes = Math.max(curRes, longRes);
            }
        }
        return longRes;

    }
}
